package com.nixsolutions.tag;

import com.nixsolutions.bean.User;
import com.nixsolutions.dao.UserDao;
import java.io.IOException;
import java.util.List;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author yakimovich
 */
public class TableUsersTag extends TagSupport {

    private static final long serialVersionUID = 1L;

    @Autowired
    private UserDao userDao;
    
    private List<User> users;
    /**
     * Create logger.
     */
    private static org.apache.commons.logging.Log logger = LogFactory.getLog(TableUsersTag.class);

    /**
     * Print table users.
     *
     * @return
     */
    @Override
    public int doStartTag() {
        JspWriter writer = pageContext.getOut();
        users = userDao.findAll();
        try {
            writer.print("<table width=\"70%\" border=\"1\" "
                    + "cellspacing=\"0\" cellpadding=\"4\" "
                    + "align=\"center\"> ");
            writer.print("<tr>");

            writer.print("<th>");
            writer.print("Login");
            writer.print("</th>");

            writer.print("<th>");
            writer.print("First Name");
            writer.print("</th>");

            writer.print("<th>");
            writer.print("Last Name");
            writer.print("</th>");

            writer.print("<th>");
            writer.print("Age");
            writer.print("</th>");

            writer.print("<th>");
            writer.print("Role");
            writer.print("</th>");

            writer.print("<th>");
            writer.print("Actions");
            writer.print("</th>");

            writer.print("</tr>");
            for (User user : users) {
                writer.print("<tr>");

                writer.print("<td>");
                writer.print(user.getLogin());
                writer.print("</td>");

                writer.print("<td>");
                writer.print(user.getFirstName());
                writer.print("</td>");

                writer.print("<td>");
                writer.print(user.getLastName());
                writer.print("</td>");

                writer.print("<td>");
                writer.print("<center>");
                writer.print(user.getAge());
                writer.print("</center>");
                writer.print("</td>");

                writer.print("<td>");
                writer.print(user.getRole().getName());
                writer.print("</td>");

                writer.print("<td>");
                writer.print("<center>");
                writer.print("<a href='EditUser?login="
                        + user.getLogin() + "'>" + " Edit </a>");
                writer.print("<a href='RemoveUser?id="
                        + user.getId() + "&login=" + user.getLogin()
                        + "' onclick='return confirm(\"Are you sure?\")'>"
                        + " Delete </a>");
                writer.print("</center>");
                writer.print("</td>");
                writer.print("</tr>");
            }
            writer.print("</table>");

        } catch (IOException e) {
            logger.error(this, e);
        }
        return EVAL_BODY_INCLUDE;
    }
}
